package cn.zf233.springsecurityjwt.config.handler;

import cn.zf233.springsecurityjwt.common.R;
import cn.zf233.springsecurityjwt.util.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zf233 on 2021/7/12
 */
@Slf4j
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        String uri = request.getRequestURI();
        log.info("请求访问：{}，认证失败，无法访问系统资源", uri);
        ServletUtil.renderString(response, R.createByErrorMessage("认证失败，无法访问资源:" + uri));
    }
}
